home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / libbfd.c < prev    next >
C/C++ Source or Header  |  1994-09-26  |  21KB  |  881 lines

  1. /* Assorted BFD support routines, only used internally.
  2.    Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24.  
  25. /*
  26. SECTION
  27.     Internal functions
  28.  
  29. DESCRIPTION
  30.     These routines are used within BFD.
  31.     They are not intended for export, but are documented here for
  32.     completeness.
  33. */
  34.  
  35. /* A routine which is used in target vectors for unsupported
  36.    operations.  */
  37.  
  38. /*ARGSUSED*/
  39. boolean
  40. bfd_false (ignore)
  41.      bfd *ignore;
  42. {
  43.   bfd_set_error (bfd_error_invalid_operation);
  44.   return false;
  45. }
  46.  
  47. /* A routine which is used in target vectors for supported operations
  48.    which do not actually do anything.  */
  49.  
  50. /*ARGSUSED*/
  51. boolean
  52. bfd_true (ignore)
  53.      bfd *ignore;
  54. {
  55.   return true;
  56. }
  57.  
  58. /* A routine which is used in target vectors for unsupported
  59.    operations which return a pointer value.  */
  60.  
  61. /*ARGSUSED*/
  62. PTR
  63. bfd_nullvoidptr (ignore)
  64.      bfd *ignore;
  65. {
  66.   bfd_set_error (bfd_error_invalid_operation);
  67.   return NULL;
  68. }
  69.  
  70. /*ARGSUSED*/
  71. int 
  72. bfd_0 (ignore)
  73.      bfd *ignore;
  74. {
  75.   return 0;
  76. }
  77.  
  78. /*ARGSUSED*/
  79. unsigned int 
  80. bfd_0u (ignore)
  81.      bfd *ignore;
  82. {
  83.    return 0;
  84. }
  85.  
  86. /*ARGUSED*/
  87. long
  88. bfd_0l (ignore)
  89.      bfd *ignore;
  90. {
  91.   return 0;
  92. }
  93.  
  94. /* A routine which is used in target vectors for unsupported
  95.    operations which return -1 on error.  */
  96.  
  97. /*ARGSUSED*/
  98. long
  99. _bfd_n1 (ignore_abfd)
  100.      bfd *ignore_abfd;
  101. {
  102.   bfd_set_error (bfd_error_invalid_operation);
  103.   return -1;
  104. }
  105.  
  106. /*ARGSUSED*/
  107. void 
  108. bfd_void (ignore)
  109.      bfd *ignore;
  110. {
  111. }
  112.  
  113. /*ARGSUSED*/
  114. boolean
  115. _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
  116.      bfd *ignore_core_bfd;
  117.      bfd *ignore_exec_bfd;
  118. {
  119.   bfd_set_error (bfd_error_invalid_operation);
  120.   return false;
  121. }
  122.  
  123. /* Routine to handle core_file_failing_command entry point for targets
  124.    without core file support.  */
  125.  
  126. /*ARGSUSED*/
  127. char *
  128. _bfd_nocore_core_file_failing_command (ignore_abfd)
  129.      bfd *ignore_abfd;
  130. {
  131.   bfd_set_error (bfd_error_invalid_operation);
  132.   return (char *)NULL;
  133. }
  134.  
  135. /* Routine to handle core_file_failing_signal entry point for targets
  136.    without core file support.  */
  137.  
  138. /*ARGSUSED*/
  139. int
  140. _bfd_nocore_core_file_failing_signal (ignore_abfd)
  141.      bfd *ignore_abfd;
  142. {
  143.   bfd_set_error (bfd_error_invalid_operation);
  144.   return 0;
  145. }
  146.  
  147. /*ARGSUSED*/
  148. const bfd_target *
  149. _bfd_dummy_target (ignore_abfd)
  150.      bfd *ignore_abfd;
  151. {
  152.   bfd_set_error (bfd_error_wrong_format);
  153.   return 0;
  154. }
  155.  
  156.  
  157. #ifndef bfd_zmalloc
  158. /* allocate and clear storage */
  159.  
  160. char *
  161. bfd_zmalloc (size)
  162.      bfd_size_type size;
  163. {
  164.   char *ptr = (char *) malloc ((size_t) size);
  165.  
  166.   if (ptr && size)
  167.    memset(ptr, 0, (size_t) size);
  168.  
  169.   return ptr;
  170. }
  171. #endif /* bfd_zmalloc */
  172.  
  173. /* Some IO code */
  174.  
  175.  
  176. /* Note that archive entries don't have streams; they share their parent's.
  177.    This allows someone to play with the iostream behind BFD's back.
  178.  
  179.    Also, note that the origin pointer points to the beginning of a file's
  180.    contents (0 for non-archive elements).  For archive entries this is the
  181.    first octet in the file, NOT the beginning of the archive header. */
  182.  
  183. static 
  184. int
  185. real_read (where, a,b, file)
  186.      PTR where;
  187.      int a;
  188.      int b;
  189.      FILE *file;
  190. {
  191.   return fread(where, a,b,file);
  192. }
  193.  
  194. /* Return value is amount read (FIXME: how are errors and end of file dealt
  195.    with?  We never call bfd_set_error, which is probably a mistake).  */
  196.  
  197. bfd_size_type
  198. bfd_read (ptr, size, nitems, abfd)
  199.      PTR ptr;
  200.      bfd_size_type size;
  201.      bfd_size_type nitems;
  202.      bfd *abfd;
  203. {
  204.   int nread;
  205.   nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
  206. #ifdef FILE_OFFSET_IS_CHAR_INDEX
  207.   if (nread > 0)
  208.     abfd->where += nread;
  209. #endif
  210.  
  211.   /* Set bfd_error if we did not read as much data as we expected.
  212.  
  213.      If the read failed due to an error set the bfd_error_system_call,
  214.      else set bfd_error_file_truncated.
  215.  
  216.      A BFD backend may wish to override bfd_error_file_truncated to
  217.      provide something more useful (eg. no_symbols or wrong_format).  */
  218.   if (nread < (int)(size * nitems))
  219.     {
  220.       if (ferror (bfd_cache_lookup (abfd)))
  221.     bfd_set_error (bfd_error_system_call);
  222.       else
  223.     bfd_set_error (bfd_error_file_truncated);
  224.     }
  225.  
  226.   return nread;
  227. }
  228.  
  229. bfd_size_type
  230. bfd_write (ptr, size, nitems, abfd)
  231.      CONST PTR ptr;
  232.      bfd_size_type size;
  233.      bfd_size_type nitems;
  234.      bfd *abfd;
  235. {
  236.   int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd));
  237. #ifdef FILE_OFFSET_IS_CHAR_INDEX
  238.   if (nwrote > 0)
  239.     abfd->where += nwrote;
  240. #endif
  241.   if (nwrote != size * nitems)
  242.     {
  243. #ifdef ENOSPC
  244.       if (nwrote >= 0)
  245.     errno = ENOSPC;
  246. #endif
  247.       bfd_set_error (bfd_error_system_call);
  248.     }
  249.   return nwrote;
  250. }
  251.  
  252. /*
  253. INTERNAL_FUNCTION
  254.     bfd_write_bigendian_4byte_int
  255.  
  256. SYNOPSIS
  257.     void bfd_write_bigendian_4byte_int(bfd *abfd,  int i);
  258.  
  259. DESCRIPTION
  260.     Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
  261.     endian order regardless of what else is going on.  This is useful in
  262.     archives.
  263.  
  264. */
  265. void
  266. bfd_write_bigendian_4byte_int (abfd, i)
  267.      bfd *abfd;
  268.      int i;
  269. {
  270.   bfd_byte buffer[4];
  271.   bfd_putb32(i, buffer);
  272.   if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
  273.     abort ();
  274. }
  275.  
  276. long
  277. bfd_tell (abfd)
  278.      bfd *abfd;
  279. {
  280.   file_ptr ptr;
  281.  
  282.   ptr = ftell (bfd_cache_lookup(abfd));
  283.  
  284.   if (abfd->my_archive)
  285.     ptr -= abfd->origin;
  286.   abfd->where = ptr;
  287.   return ptr;
  288. }
  289.  
  290. int
  291. bfd_flush (abfd)
  292.      bfd *abfd;
  293. {
  294.   return fflush (bfd_cache_lookup(abfd));
  295. }
  296.  
  297. int
  298. bfd_stat (abfd, statbuf)
  299.      bfd *abfd;
  300.      struct stat *statbuf;
  301. {
  302.   return fstat (fileno(bfd_cache_lookup(abfd)), statbuf);
  303. }
  304.  
  305. /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
  306.    can retrieve the error code).  */
  307.  
  308. int
  309. bfd_seek (abfd, position, direction)
  310.      bfd *abfd;
  311.      file_ptr position;
  312.      int direction;
  313. {
  314.   int result;
  315.   FILE *f;
  316.   file_ptr file_position;
  317.   /* For the time being, a BFD may not seek to it's end.  The problem
  318.      is that we don't easily have a way to recognize the end of an
  319.      element in an archive. */
  320.  
  321.   BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
  322.  
  323.   if (direction == SEEK_CUR && position == 0)
  324.     return 0;
  325. #ifdef FILE_OFFSET_IS_CHAR_INDEX
  326.   if (abfd->format != bfd_archive && abfd->my_archive == 0)
  327.     {
  328. #if 0
  329.       /* Explanation for this code: I'm only about 95+% sure that the above
  330.      conditions are sufficient and that all i/o calls are properly
  331.      adjusting the `where' field.  So this is sort of an `assert'
  332.      that the `where' field is correct.  If we can go a while without
  333.      tripping the abort, we can probably safely disable this code,
  334.      so that the real optimizations happen.  */
  335.       file_ptr where_am_i_now;
  336.       where_am_i_now = ftell (bfd_cache_lookup (abfd));
  337.       if (abfd->my_archive)
  338.     where_am_i_now -= abfd->origin;
  339.       if (where_am_i_now != abfd->where)
  340.     abort ();
  341. #endif
  342.       if (direction == SEEK_SET && position == abfd->where)
  343.     return 0;
  344.     }
  345.   else
  346.     {
  347.       /* We need something smarter to optimize access to archives.
  348.      Currently, anything inside an archive is read via the file
  349.      handle for the archive.  Which means that a bfd_seek on one
  350.      component affects the `current position' in the archive, as
  351.      well as in any other component.
  352.  
  353.      It might be sufficient to put a spike through the cache
  354.      abstraction, and look to the archive for the file position,
  355.      but I think we should try for something cleaner.
  356.  
  357.      In the meantime, no optimization for archives.  */
  358.     }
  359. #endif
  360.  
  361.   f = bfd_cache_lookup (abfd);
  362.   file_position = position;
  363.   if (direction == SEEK_SET && abfd->my_archive != NULL)
  364.     file_position += abfd->origin;
  365.  
  366.   result = fseek (f, file_position, direction);
  367.  
  368.   if (result != 0)
  369.     {
  370.       /* Force redetermination of `where' field.  */
  371.       bfd_tell (abfd);
  372.       bfd_set_error (bfd_error_system_call);
  373.     }
  374.   else
  375.     {
  376. #ifdef FILE_OFFSET_IS_CHAR_INDEX
  377.       /* Adjust `where' field.  */
  378.       if (direction == SEEK_SET)
  379.     abfd->where = position;
  380.       else
  381.     abfd->where += position;
  382. #endif
  383.     }
  384.   return result;
  385. }
  386.  
  387. /** Make a string table */
  388.  
  389. /*>bfd.h<
  390.  Add string to table pointed to by table, at location starting with free_ptr.
  391.    resizes the table if necessary (if it's NULL, creates it, ignoring
  392.    table_length).  Updates free_ptr, table, table_length */
  393.  
  394. boolean
  395. bfd_add_to_string_table (table, new_string, table_length, free_ptr)
  396.      char **table;
  397.      char *new_string;
  398.      unsigned int *table_length;
  399.      char **free_ptr;
  400. {
  401.   size_t string_length = strlen (new_string) + 1; /* include null here */
  402.   char *base = *table;
  403.   size_t space_length = *table_length;
  404.   unsigned int offset = (base ? *free_ptr - base : 0);
  405.  
  406.   if (base == NULL) {
  407.     /* Avoid a useless regrow if we can (but of course we still
  408.        take it next time).  */
  409.     space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
  410.                     DEFAULT_STRING_SPACE_SIZE : string_length+1);
  411.     base = bfd_zmalloc ((bfd_size_type) space_length);
  412.  
  413.     if (base == NULL) {
  414.       bfd_set_error (bfd_error_no_memory);
  415.       return false;
  416.     }
  417.   }
  418.  
  419.   if ((size_t)(offset + string_length) >= space_length) {
  420.     /* Make sure we will have enough space */
  421.     while ((size_t)(offset + string_length) >= space_length) 
  422.       space_length += space_length/2; /* grow by 50% */
  423.  
  424.     base = (char *) realloc (base, space_length);
  425.     if (base == NULL) {
  426.       bfd_set_error (bfd_error_no_memory);
  427.       return false;
  428.     }
  429.  
  430.   }
  431.  
  432.   memcpy (base + offset, new_string, string_length);
  433.   *table = base;
  434.   *table_length = space_length;
  435.   *free_ptr = base + offset + string_length;
  436.   
  437.   return true;
  438. }
  439.  
  440. /** The do-it-yourself (byte) sex-change kit */
  441.  
  442. /* The middle letter e.g. get<b>short indicates Big or Little endian
  443.    target machine.  It doesn't matter what the byte order of the host
  444.    machine is; these routines work for either.  */
  445.  
  446. /* FIXME: Should these take a count argument?
  447.    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
  448.                              functions in swap.h #ifdef __GNUC__. 
  449.                              Gprof them later and find out.  */
  450.  
  451. /*
  452. FUNCTION
  453.     bfd_put_size
  454. FUNCTION
  455.     bfd_get_size
  456.  
  457. DESCRIPTION
  458.     These macros as used for reading and writing raw data in
  459.     sections; each access (except for bytes) is vectored through
  460.     the target format of the BFD and mangled accordingly. The
  461.     mangling performs any necessary endian translations and
  462.     removes alignment restrictions.  Note that types accepted and
  463.     returned by these macros are identical so they can be swapped
  464.     around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
  465.     to either <<bfd_get_32>> or <<bfd_get_64>>.
  466.  
  467.     In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
  468.     system without prototypes, the caller is responsible for making
  469.     sure that is true, with a cast if necessary.  We don't cast
  470.     them in the macro definitions because that would prevent <<lint>>
  471.     or <<gcc -Wall>> from detecting sins such as passing a pointer.
  472.     To detect calling these with less than a <<bfd_vma>>, use
  473.     <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
  474.  
  475. .
  476. .{* Byte swapping macros for user section data.  *}
  477. .
  478. .#define bfd_put_8(abfd, val, ptr) \
  479. .                (*((unsigned char *)(ptr)) = (unsigned char)(val))
  480. .#define bfd_put_signed_8 \
  481. .        bfd_put_8
  482. .#define bfd_get_8(abfd, ptr) \
  483. .                (*(unsigned char *)(ptr))
  484. .#define bfd_get_signed_8(abfd, ptr) \
  485. .        ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
  486. .
  487. .#define bfd_put_16(abfd, val, ptr) \
  488. .                BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
  489. .#define bfd_put_signed_16 \
  490. .         bfd_put_16
  491. .#define bfd_get_16(abfd, ptr) \
  492. .                BFD_SEND(abfd, bfd_getx16, (ptr))
  493. .#define bfd_get_signed_16(abfd, ptr) \
  494. .              BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
  495. .
  496. .#define bfd_put_32(abfd, val, ptr) \
  497. .                BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
  498. .#define bfd_put_signed_32 \
  499. .         bfd_put_32
  500. .#define bfd_get_32(abfd, ptr) \
  501. .                BFD_SEND(abfd, bfd_getx32, (ptr))
  502. .#define bfd_get_signed_32(abfd, ptr) \
  503. .         BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
  504. .
  505. .#define bfd_put_64(abfd, val, ptr) \
  506. .                BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
  507. .#define bfd_put_signed_64 \
  508. .         bfd_put_64
  509. .#define bfd_get_64(abfd, ptr) \
  510. .                BFD_SEND(abfd, bfd_getx64, (ptr))
  511. .#define bfd_get_signed_64(abfd, ptr) \
  512. .         BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
  513. .
  514. */ 
  515.  
  516. /*
  517. FUNCTION
  518.     bfd_h_put_size
  519.     bfd_h_get_size
  520.  
  521. DESCRIPTION
  522.     These macros have the same function as their <<bfd_get_x>>
  523.     bretheren, except that they are used for removing information
  524.     for the header records of object files. Believe it or not,
  525.     some object files keep their header records in big endian
  526.     order and their data in little endian order.
  527. .
  528. .{* Byte swapping macros for file header data.  *}
  529. .
  530. .#define bfd_h_put_8(abfd, val, ptr) \
  531. .        bfd_put_8 (abfd, val, ptr)
  532. .#define bfd_h_put_signed_8(abfd, val, ptr) \
  533. .        bfd_put_8 (abfd, val, ptr)
  534. .#define bfd_h_get_8(abfd, ptr) \
  535. .        bfd_get_8 (abfd, ptr)
  536. .#define bfd_h_get_signed_8(abfd, ptr) \
  537. .        bfd_get_signed_8 (abfd, ptr)
  538. .
  539. .#define bfd_h_put_16(abfd, val, ptr) \
  540. .                BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
  541. .#define bfd_h_put_signed_16 \
  542. .         bfd_h_put_16
  543. .#define bfd_h_get_16(abfd, ptr) \
  544. .                BFD_SEND(abfd, bfd_h_getx16,(ptr))
  545. .#define bfd_h_get_signed_16(abfd, ptr) \
  546. .         BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
  547. .
  548. .#define bfd_h_put_32(abfd, val, ptr) \
  549. .                BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
  550. .#define bfd_h_put_signed_32 \
  551. .         bfd_h_put_32
  552. .#define bfd_h_get_32(abfd, ptr) \
  553. .                BFD_SEND(abfd, bfd_h_getx32,(ptr))
  554. .#define bfd_h_get_signed_32(abfd, ptr) \
  555. .         BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
  556. .
  557. .#define bfd_h_put_64(abfd, val, ptr) \
  558. .                BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
  559. .#define bfd_h_put_signed_64 \
  560. .         bfd_h_put_64
  561. .#define bfd_h_get_64(abfd, ptr) \
  562. .                BFD_SEND(abfd, bfd_h_getx64,(ptr))
  563. .#define bfd_h_get_signed_64(abfd, ptr) \
  564. .         BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
  565. .
  566. */ 
  567.  
  568. /* Sign extension to bfd_signed_vma.  */
  569. #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
  570. #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
  571. #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
  572. #define COERCE64(x) \
  573.   (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
  574.  
  575. bfd_vma
  576. bfd_getb16 (addr)
  577.      register const bfd_byte *addr;
  578. {
  579.   return (addr[0] << 8) | addr[1];
  580. }
  581.  
  582. bfd_vma
  583. bfd_getl16 (addr)
  584.      register const bfd_byte *addr;
  585. {
  586.   return (addr[1] << 8) | addr[0];
  587. }
  588.  
  589. bfd_signed_vma
  590. bfd_getb_signed_16 (addr)
  591.      register const bfd_byte *addr;
  592. {
  593.   return COERCE16((addr[0] << 8) | addr[1]);
  594. }
  595.  
  596. bfd_signed_vma
  597. bfd_getl_signed_16 (addr)
  598.      register const bfd_byte *addr;
  599. {
  600.   return COERCE16((addr[1] << 8) | addr[0]);
  601. }
  602.  
  603. void
  604. bfd_putb16 (data, addr)
  605.      bfd_vma data;
  606.      register bfd_byte *addr;
  607. {
  608.   addr[0] = (bfd_byte)(data >> 8);
  609.   addr[1] = (bfd_byte )data;
  610. }
  611.  
  612. void
  613. bfd_putl16 (data, addr)
  614.      bfd_vma data;             
  615.      register bfd_byte *addr;
  616. {
  617.   addr[0] = (bfd_byte )data;
  618.   addr[1] = (bfd_byte)(data >> 8);
  619. }
  620.  
  621. bfd_vma
  622. bfd_getb32 (addr)
  623.      register const bfd_byte *addr;
  624. {
  625.   return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
  626.       | addr[2]) << 8 | addr[3];
  627. }
  628.  
  629. bfd_vma
  630. bfd_getl32 (addr)
  631.      register const bfd_byte *addr;
  632. {
  633.   return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
  634.       | addr[1]) << 8 | addr[0];
  635. }
  636.  
  637. bfd_signed_vma
  638. bfd_getb_signed_32 (addr)
  639.      register const bfd_byte *addr;
  640. {
  641.   return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
  642.            | addr[2]) << 8 | addr[3]);
  643. }
  644.  
  645. bfd_signed_vma
  646. bfd_getl_signed_32 (addr)
  647.      register const bfd_byte *addr;
  648. {
  649.   return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
  650.            | addr[1]) << 8 | addr[0]);
  651. }
  652.  
  653. bfd_vma
  654. bfd_getb64 (addr)
  655.      register const bfd_byte *addr;
  656. {
  657. #ifdef BFD64
  658.   bfd_vma low, high;
  659.  
  660.   high= ((((((((addr[0]) << 8) |
  661.               addr[1]) << 8) |
  662.             addr[2]) << 8) |
  663.           addr[3]) );
  664.  
  665.   low = (((((((((bfd_vma)addr[4]) << 8) |
  666.               addr[5]) << 8) |
  667.             addr[6]) << 8) |
  668.           addr[7]));
  669.  
  670.   return high << 32 | low;
  671. #else
  672.   BFD_FAIL();
  673.   return 0;
  674. #endif
  675. }
  676.  
  677. bfd_vma
  678. bfd_getl64 (addr)
  679.      register const bfd_byte *addr;
  680. {
  681. #ifdef BFD64
  682.   bfd_vma low, high;
  683.   high= (((((((addr[7] << 8) |
  684.               addr[6]) << 8) |
  685.             addr[5]) << 8) |
  686.           addr[4]));
  687.  
  688.   low = ((((((((bfd_vma)addr[3] << 8) |
  689.               addr[2]) << 8) |
  690.             addr[1]) << 8) |
  691.           addr[0]) );
  692.  
  693.   return high << 32 | low;
  694. #else
  695.   BFD_FAIL();
  696.   return 0;
  697. #endif
  698.  
  699. }
  700.  
  701. bfd_signed_vma
  702. bfd_getb_signed_64 (addr)
  703.      register const bfd_byte *addr;
  704. {
  705. #ifdef BFD64
  706.   bfd_vma low, high;
  707.  
  708.   high= ((((((((addr[0]) << 8) |
  709.               addr[1]) << 8) |
  710.             addr[2]) << 8) |
  711.           addr[3]) );
  712.  
  713.   low = (((((((((bfd_vma)addr[4]) << 8) |
  714.               addr[5]) << 8) |
  715.             addr[6]) << 8) |
  716.           addr[7]));
  717.  
  718.   return COERCE64(high << 32 | low);
  719. #else
  720.   BFD_FAIL();
  721.   return 0;
  722. #endif
  723. }
  724.  
  725. bfd_signed_vma
  726. bfd_getl_signed_64 (addr)
  727.      register const bfd_byte *addr;
  728. {
  729. #ifdef BFD64
  730.   bfd_vma low, high;
  731.   high= (((((((addr[7] << 8) |
  732.               addr[6]) << 8) |
  733.             addr[5]) << 8) |
  734.           addr[4]));
  735.  
  736.   low = ((((((((bfd_vma)addr[3] << 8) |
  737.               addr[2]) << 8) |
  738.             addr[1]) << 8) |
  739.           addr[0]) );
  740.  
  741.   return COERCE64(high << 32 | low);
  742. #else
  743.   BFD_FAIL();
  744.   return 0;
  745. #endif
  746. }
  747.  
  748. void
  749. bfd_putb32 (data, addr)
  750.      bfd_vma data;
  751.      register bfd_byte *addr;
  752. {
  753.         addr[0] = (bfd_byte)(data >> 24);
  754.         addr[1] = (bfd_byte)(data >> 16);
  755.         addr[2] = (bfd_byte)(data >>  8);
  756.         addr[3] = (bfd_byte)data;
  757. }
  758.  
  759. void
  760. bfd_putl32 (data, addr)
  761.      bfd_vma data;
  762.      register bfd_byte *addr;
  763. {
  764.         addr[0] = (bfd_byte)data;
  765.         addr[1] = (bfd_byte)(data >>  8);
  766.         addr[2] = (bfd_byte)(data >> 16);
  767.         addr[3] = (bfd_byte)(data >> 24);
  768. }
  769.  
  770. void
  771. bfd_putb64 (data, addr)
  772.      bfd_vma data;
  773.      register bfd_byte *addr;
  774. {
  775. #ifdef BFD64
  776.   addr[0] = (bfd_byte)(data >> (7*8));
  777.   addr[1] = (bfd_byte)(data >> (6*8));
  778.   addr[2] = (bfd_byte)(data >> (5*8));
  779.   addr[3] = (bfd_byte)(data >> (4*8));
  780.   addr[4] = (bfd_byte)(data >> (3*8));
  781.   addr[5] = (bfd_byte)(data >> (2*8));
  782.   addr[6] = (bfd_byte)(data >> (1*8));
  783.   addr[7] = (bfd_byte)(data >> (0*8));
  784. #else
  785.   BFD_FAIL();
  786. #endif
  787. }
  788.  
  789. void
  790. bfd_putl64 (data, addr)
  791.      bfd_vma data;
  792.      register bfd_byte *addr;
  793. {
  794. #ifdef BFD64
  795.   addr[7] = (bfd_byte)(data >> (7*8));
  796.   addr[6] = (bfd_byte)(data >> (6*8));
  797.   addr[5] = (bfd_byte)(data >> (5*8));
  798.   addr[4] = (bfd_byte)(data >> (4*8));
  799.   addr[3] = (bfd_byte)(data >> (3*8));
  800.   addr[2] = (bfd_byte)(data >> (2*8));
  801.   addr[1] = (bfd_byte)(data >> (1*8));
  802.   addr[0] = (bfd_byte)(data >> (0*8));
  803. #else
  804.   BFD_FAIL();
  805. #endif
  806. }
  807.  
  808. /* Default implementation */
  809.  
  810. boolean
  811. _bfd_generic_get_section_contents (abfd, section, location, offset, count)
  812.      bfd *abfd;
  813.      sec_ptr section;
  814.      PTR location;
  815.      file_ptr offset;
  816.      bfd_size_type count;
  817. {
  818.     if (count == 0)
  819.         return true;
  820.     if ((bfd_size_type)(offset+count) > section->_raw_size
  821.         || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
  822.         || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
  823.         return (false); /* on error */
  824.     return (true);
  825. }
  826.  
  827. /* This generic function can only be used in implementations where creating
  828.    NEW sections is disallowed.  It is useful in patching existing sections
  829.    in read-write files, though.  See other set_section_contents functions
  830.    to see why it doesn't work for new sections.  */
  831. boolean
  832. _bfd_generic_set_section_contents (abfd, section, location, offset, count)
  833.      bfd *abfd;
  834.      sec_ptr section;
  835.      PTR location;
  836.      file_ptr offset;
  837.      bfd_size_type count;
  838. {
  839.   if (count == 0)
  840.     return true;
  841.  
  842.   if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
  843.       || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
  844.     return false;
  845.  
  846.   return true;
  847. }
  848.  
  849. /*
  850. INTERNAL_FUNCTION
  851.     bfd_log2
  852.  
  853. SYNOPSIS
  854.     unsigned int bfd_log2(bfd_vma x);
  855.  
  856. DESCRIPTION
  857.     Return the log base 2 of the value supplied, rounded up.  E.g., an
  858.     @var{x} of 1025 returns 11.
  859. */
  860.  
  861. unsigned
  862. bfd_log2(x)
  863.      bfd_vma x;
  864. {
  865.   unsigned result = 0;
  866.   while ( (bfd_vma)(1<< result) < x)
  867.     result++;
  868.   return result;
  869. }
  870.  
  871. boolean
  872. bfd_generic_is_local_label (abfd, sym)
  873.      bfd *abfd;
  874.      asymbol *sym;
  875. {
  876.   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
  877.  
  878.   return (sym->name[0] == locals_prefix);
  879. }
  880.  
  881.